home *** CD-ROM | disk | FTP | other *** search
Text File | 1989-10-09 | 3.5 KB | 153 lines | [TEXT/GEOL] |
- Item 9116153 8-Oct-89 09:41
-
- From: SCHMUCKER1 Schmucker, Kurt
-
- To: D0738 Kane Biomedical System, S Helm,PRT
-
- cc: MACAPP.TECH$ MACAPP Tech
- DEREK White, Derek
- NASSI Nassi, Ike
- APPLE.BUGS Apple Bugs Reporting
-
- Sub: Re: Object Pascal Question
-
- Jeffrey,
-
- You are correct that it is not easy to do what you want (have mutual
- references between two Object Pascal classes) -- but it should be! The
- following small program illustrates the problem:
-
- ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- PROGRAM MutualRefTest;
-
- USES
- ObjIntf;
-
- TYPE
-
- TFooObject = OBJECT (TObject)
- fMyFrob:TFrobObject;
- PROCEDURE TFooObject.IFooObject;
- END;
-
- TFrobObject = OBJECT(TObject)
- fMyFoo: TFooObject;
- PROCEDURE TFrobObject.IFrobObject;
- END;
-
-
- VAR
- aFoo: TFooObject;
- aFrob: TFrobObject;
-
-
- PROCEDURE TFooObject.IFooObject;
- BEGIN
- END;
-
- PROCEDURE TFrobObject.IFrobObject;
- BEGIN
- END;
-
- BEGIN
- NEW(aFoo);
- NEW(aFrob);
-
- aFrob.fMyFoo := aFoo;
- aFoo.fMyFrob := aFrob;
- END.
-
- ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
-
- Compiling this results in the following Pascal error:
-
- pascal MutualRefTest.p
- # aFrob.fMyFoo := aFoo;
- # aFoo.fMyFrob := aFrob;
- # ?
- ### pascal - Error 144 Type conflict of operands
- #-----------------------------------------------------------------------
- File "MutualRefTest.p"; Line 37
- #-----------------------------------------------------------------------
-
-
- HOWEVER, reversing the order of the definitions of TFoo and TFrob results in
- the following error:
-
- #
- # aFrob.fMyFoo := aFoo;
- # ?
- ### pascal - Error 144 Type conflict of operands
- #-------------------------------------------------------------------------
- File "MutualRefTest.p"; Line 35
- #-------------------------------------------------------------------------
-
-
-
-
- I believe that this problem has been corrected in MPW Pascal 3.1 which is
- now in testing. When I get my copy (in a week or so, I hope), I will re-run
- this test and post the result.
-
-
- In the meantime, the solution appears to be to declare (at least) the first
- such mutual reference as a TObject, and then cast the value whenever you use
- it. (Shown below.) I don't think that your suggestion of using Member is the
- way to proceed. Except in very rare cases (some of which can be seen in
- UDialog), Member is usually a mistake as a way out of a difficult problem or,
- in this case, a compiler glitch.
-
- In terms of making your job easier, it might be best to just have BOTH
- references defined as TObjects, and always cast every use, rather than having
- to always remember which of them was declared first.
-
-
- The Workaround
-
- PROGRAM MutualRefTestFix;
-
- USES
- ObjIntf;
-
- TYPE
-
- TFooObject = OBJECT (TObject)
- fMyFrob:TObject; { ••••••••••• Note change ••••••••••• }
- PROCEDURE TFooObject.IFooObject;
- END;
-
- TFrobObject = OBJECT(TObject)
- fMyFoo: TFooObject;
- PROCEDURE TFrobObject.IFrobObject;
- END;
-
-
- VAR
- aFoo: TFooObject;
- aFrob: TFrobObject;
-
-
- PROCEDURE TFooObject.IFooObject;
- BEGIN
- END;
-
- PROCEDURE TFrobObject.IFrobObject;
- BEGIN
- END;
-
- BEGIN
- NEW(aFoo);
- NEW(aFrob);
-
- aFrob.fMyFoo := aFoo;
- aFoo.fMyFrob := aFrob;
-
- { When USING the value of fMyFrob, always cast it. } { ••• Change •••• }
- TFrobObject(aFoo.fMyFrob).IFrobObject;
- END.
-
-
-
- Kurt
-
-